<template>
  <div class="header_box">
    <div class="text_box">欢迎登录自定义管理系统</div>
    <div class="user_box">
      <span class="username_box">{{ userInfo.nickname }}</span>
      <el-dropdown>
        <span class="el-dropdown-link">
          <el-avatar :src="userInfo.avatar"></el-avatar>
          <i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="$router.push('/user_center')">个人中心</el-dropdown-item>
          <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
      <div class="link_box">
        <el-link type="danger" :underline="false" href="https://space.bilibili.com/501596583" target="_blank">B站</el-link>
        <el-link type="danger" :underline="false" href="https://i.csdn.net/#/user-center/profile?spm=1000.2115.3001.5111" target="_blank">CSDN</el-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Header",
  data() {
    return {
      userInfo: {
        nickname: '',
        avatar: ''
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getUserInfo() {
      //从sessionStorage中获取用户信息
      this.$axios.get("/user/getUser/" + sessionStorage.getItem("userId")).then(res => {
        this.userInfo = res.data.data
      })
    },
    logout() {
      //弹窗提示
      this.$confirm('是否确认退出?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '欢迎下次登录!'
        })
        //清空localStorage
        localStorage.clear()
        //清空sessionStorage
        sessionStorage.clear()
        this.$router.push('/login')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消成功'
        });
      });
    }
  }
}
</script>

<style scoped>
.username_box {
  color: rgba(27, 122, 114, 0.73);
}

.el-dropdown {
  margin-left: 10px;
}

.link_box {
  width: 100px;
  display: flex;
  justify-content: space-around;
  margin-left: 10px;
}

.el-avatar {
  margin-top: 10px;
}

.el-dropdown-link {
  cursor: pointer;
}
.el-icon-arrow-down {
  font-size: 12px;
}

.text_box {
  margin-left: 600px;
  font-size: 20px;
  color: rgba(33, 31, 107, 0.73);
  font-weight: bold;
}

.header_box {
  display: flex;
  justify-content: space-between;
}

.user_box {
  display: flex;
}
</style>